﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Diagnostics;
using System.Xml.Linq;
using System.Threading;

namespace STRobot
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Config.Get().InitFromFile();
            this.tabControl1.SetBounds(4, 4, this.Width - 8, this.Height - 8);
            initShow();
            //init stapp.dll

        }

        private void initShow()
        {
            if (Config.Get().notpickupitem != null)
            {
                textBox_notpickupitem.Text = string.Join(",", Config.Get().notpickupitem);
            }
            if (Config.Get().servername != null)
            {
                textBox1.Text = Config.Get().servername;
            }
            if (Config.Get().rolename != null)
            {
                textBox2.Text = Config.Get().rolename;
            }
          
            comboBox1.SelectedIndex = Config.Get().attackmode != -1 ? Config.Get().attackmode : -1;
            textBox3.Text = Config.Get().monsterlevel != 0 ? Config.Get().monsterlevel.ToString() : "1";
            checkBox3.Checked = Config.Get().autohuishou > 0 ? true : false;
            bigmonsterlistShow();
            petinfolistshow();
            guajipointsshow();
            jumppointsshow();
            showhomelist();
        }
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            this.tabControl1.SetBounds(4,4, this.Width - 8, this.Height - 8);
        }


        private void bigmonsterlistShow()
        {
            if (Config.Get().bigmonsterlist == null)
            {
                return;
            }
            listBox2.Items.Clear();
            var id = 1;
            
            foreach (var item in Config.Get().bigmonsterlist)
            {
                string str = (item.type == 1 ? "✓ " : "✕ ") + "  " + (item.tid == 0? "000": item.tid.ToString()) + "  " + item.name + "     " + item.level.ToString();
                listBox2.Items.Add(str);
                id++;
            }
        }
        private void petinfolistshow()
        {
            if (Config.Get().petinfo == null)
            {
                return;
            }
            listBox1.Items.Clear();
            var id = 1;
           
            foreach (var item in Config.Get().petinfo)
            {
                listBox1.Items.Add("" + item.name + ",双击查看");
                id++;
            }
        }
        //挂机点列表刷新
        private void guajipointsshow()
        {
            if (Config.Get().guajipoint == null)
            {
                return;
            }
            listBox3.Items.Clear();
            
            foreach (var item in Config.Get().guajipoint)
            {
                listBox3.Items.Add((item.timeout != -1 ? "✓ ": "✕ ") + item.showname + ",刷新:" + item.flushtime.ToString() + "秒,停留"+ item.idletime.ToString() +"秒,详情双击查看");
            }
        }
        //NPC跳转点显示
        private void jumppointsshow()
        {
            if (Config.Get().jumppoints == null)
            {
                return;
            }
            listBox4.Items.Clear();
            foreach (var item in Config.Get().jumppoints)
            {
                string str = "地图异常null";
                if (item.starmapname != null)
                {
                    str = item.starmapname;
                    
                }
                listBox4.Items.Add(str);
            }
        }
        private void label6_MouseEnter(object sender, EventArgs e)//怪物级别lable
        {
            toolTip1.Show("123", label6,5000);
        }

        //自动攻击开关步骤添加
        private void button3_Click(object sender, EventArgs e)
        {
            var wnd = new UiClickAddWnd(Config.Get().autoswitch);
            wnd.Text = "内置自动攻击设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                Config.Get().autoswitch = wnd.GetUiclicks();
            }
                
        }

        //回收步骤添加
        private void button4_Click(object sender, EventArgs e)
        {
            var wnd = new UiClickAddWnd(Config.Get().huishou);
            wnd.Text = "回收设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                Config.Get().huishou = wnd.GetUiclicks();
            }
        }


        //总保存配置到文件
        private void button7_Click(object sender, EventArgs e)
        {
            Config.Get().levelpriority = 1; //开启级别优先功能
            Config.Get().emptyaround = 6;
            Config.Get().autohuishou = checkBox3.Checked ? 1 : 0;
            Config.Get().monsterlevel = textBox3.Text.Length > 0 ? Convert.ToInt32(textBox3.Text) : 1;
            Config.Get().idletime = textBox4.Text.Length > 0 ? Convert.ToInt32(textBox4.Text) : 3;
            Config.Get().otherplayer = checkBox1.Checked ? 1 : 0;
            Config.Get().attackmode = comboBox1.SelectedIndex;
            Config.Get().servername = textBox1.Text;
            Config.Get().rolename = textBox2.Text;
            Config.Get().SaveToFile();
            MessageBox.Show("保存成功"); 
        }


        //添加宠物
        private void button5_Click(object sender, EventArgs e)
        {
            var wnd = new PetAddWnd();
            wnd.Text = "宠物设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                Config.Get().petinfo.Add(wnd.GetPetinfo());
                petinfolistshow();
            }
        }
        //删除宠物
        private void button6_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1) return;

            var str = "确认删除[" + Config.Get().petinfo[listBox1.SelectedIndex].name + "]吗?";
            if (MessageBox.Show(str, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Config.Get().petinfo.RemoveAt(listBox1.SelectedIndex);
                petinfolistshow();
            }
        }

        //宠物列表双击修改
        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedIndex == -1) return;
            var wnd = new PetAddWnd(listBox1.SelectedIndex);
            wnd.Text = "宠物设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                petinfolistshow();
            }
               
        }
        //添加攻击的怪
        private void button1_Click(object sender, EventArgs e)
        {
            var wnd = new MonsterAddWnd();
            wnd.Text = "怪物列表设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                bigmonsterlistShow();
            }

        }
        //删除要攻击的怪列表
        private void button8_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex > -1)
            {
                var str = "确认删除[" + Config.Get().bigmonsterlist[listBox2.SelectedIndex].name + "]吗?";
                if (MessageBox.Show(str, "提示",MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Config.Get().bigmonsterlist.RemoveAt(listBox2.SelectedIndex);
                    bigmonsterlistShow();
                } 
            }
        }
        //大怪列表双击
        private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var wnd = new MonsterAddWnd(listBox2.SelectedIndex);
            wnd.Text = "怪物列表设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                bigmonsterlistShow();
            }
        }


        //添加检测弹窗的相关步骤
        private void button9_Click(object sender, EventArgs e)
        {
            var wnd = new UiClickAddWnd(Config.Get().tankuangclose);
            wnd.Text = "意外弹窗设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                Config.Get().tankuangclose = wnd.GetUiclicks();
            }  
        }

        //添加挂机地图
        private void button10_Click(object sender, EventArgs e)
        {
            var wnd = new GuajipointAddWnd();
            wnd.Text = "挂机点设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                guajipointsshow();
            } 
           
            // Config.Get().tankuangclose = wnd.GetUiclicks();
        }
        //双击挂机地图编辑
        private void listBox3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var wnd = new GuajipointAddWnd(listBox3.SelectedIndex);
            wnd.Text = "挂机点设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                guajipointsshow();
            }
        }
        //删除挂机点
        private void button11_Click(object sender, EventArgs e)
        {
            if (listBox3.SelectedIndex > -1)
            {
                var str = "确认删除 [" + Config.Get().guajipoint[listBox3.SelectedIndex].showname + "] 吗?";
                if (MessageBox.Show(str, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Config.Get().guajipoint.RemoveAt(listBox3.SelectedIndex);
                    guajipointsshow();
                }
            }
        }

        //保存禁止拾取物品信息
        private void button2_Click(object sender, EventArgs e)
        {
            Config.Get().pickup = checkBox2.Checked ? 1 : 0;
            var str = textBox_notpickupitem.Text;
            if (str.Length > 0)
            {
                var list =  str.Split(',');
                Config.Get().notpickupitem.Clear();
                foreach (var item in list)
                {
                    Config.Get().notpickupitem.Add(item);
                }
            }
        }

        //添加跳转地图起点
        private void button13_Click(object sender, EventArgs e)
        {
            var wnd = new MapNameAddWnd();
            wnd.Text = "跳转点设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                jumppointsshow();
            }
            
        }
        //跳转地图编辑
        private void listBox4_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var wnd = new MapNameAddWnd(listBox4.SelectedIndex);
            wnd.Text = "跳转点设置";
            if (wnd.ShowDialog() == DialogResult.OK)
            {
                jumppointsshow();
            }
        }
        //删除地图跳转信息
        private void button12_Click(object sender, EventArgs e)
        {
            if (listBox4.SelectedIndex > -1)
            {
                var str = "确认删除 [" + Config.Get().jumppoints[listBox4.SelectedIndex].starmapname + "] 吗?包含" + Config.Get().jumppoints.Count.ToString() + "个子跳转点";
                if (MessageBox.Show(str, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Config.Get().jumppoints.RemoveAt(listBox4.SelectedIndex);
                    jumppointsshow();
                }
            }
        }
        //测试按钮
        private void button14_Click(object sender, EventArgs e)
        {

            //var rect = new Rectangle(0, 0, 300, 300);
            //showRectInDesktop(ref rect,(IntPtr)0);
        }
        //添加角色技能
        private void button15_Click(object sender, EventArgs e)
        {
            var wnd = new SkillAddWnd();
            wnd.Text = "技能设置";
            wnd.ShowDialog();

        }

        private void showhomelist()
        {
            textBox5.Text = string.Join(",", Config.Get().home);
        }
        //保存主城列表
        private void button16_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认填写正确了吗?","提示",MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                var str = textBox5.Text;
                if (str.Length > 0)
                {
                    var list = str.Split(',');
                    Config.Get().home.Clear();
                    foreach (var item in list)
                    {
                        Config.Get().home.Add(item);
                    }
                }
            }
        }


        //加了这个按钮上才有弹起消息
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {

        }

        //运行脚本
        static Thread doWorkThread;
        private void button21_Click(object sender, EventArgs e)
        {
            if (doWorkThread == null)
            {
                //使用线程来调起
                doWorkThread = new Thread(() =>
                {
                    StDll.DoWork("stconfig.json");
                });
                doWorkThread.Start();
                button21.Text = "暂停";
            }
            else
            {
                if ( doWorkThread.ThreadState == System.Threading.ThreadState.Suspended)
                {
                    doWorkThread.Suspend();
                }
            }
            
        }

        private void button22_Click(object sender, EventArgs e)
        {
            if (doWorkThread != null && doWorkThread.IsAlive)
            {
                doWorkThread.Abort();
            } 
        }

        private void button17_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }
    }

    public class StDll
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate void showalluicallback(uint parent, string parentuiname, uint address, string uiname, Point pos, Point relativePos, Point size);

        [DllImport(@"ShenTuApp.dll", EntryPoint = "Initst", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public extern static int Initst(IntPtr HWND);

        [DllImport(@"ShenTuApp.dll", EntryPoint = "GetAllObjectFromString", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public extern static int GetAllObjectFromString(StringBuilder str);

        [DllImport(@"ShenTuApp.dll", EntryPoint = "ShowGameAllUi", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public extern static void ShowGameAllUi(showalluicallback pfun);

        [DllImport(@"ShenTuApp.dll", EntryPoint = "DoWork", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
        public extern static void DoWork(string configname);

        public static int CsharpCall(int a, int b)
        {
            return a * a + b * b;
        }

        public static void uicallback(uint parent, string parentuiname, uint address, string uiname, Point pos, Point relativePos, Point size)
        {
            MessageBox.Show(uiname);
        }
    }


}
